<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="模糊查询" prop="relativeName">
        <el-input
          v-model="queryParams.relativeName"
          placeholder="请输入姓名，生辰八字，电子邮箱等信息"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>

        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" fixed prop="id" />
      <el-table-column label="关联情侣用户名称" align="center"   fixed prop="nickName" />

      <el-table-column label="亲人姓名" align="center"  fixed prop="relativeName" />
      <el-form-item label="与情侣用户的关系" prop="relationToUser">
        <el-select v-model="queryParams.relationToUser" placeholder="请选择与情侣用户的关系" clearable>
          <el-option
            v-for="dict in dict.type.qiu_user_info_relation"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-table-column label="亲人性别" align="center" prop="gender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.gender"/>
        </template>
      </el-table-column>
      <el-table-column label="亲人出生日期" align="center" prop="dateOfBirth" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dateOfBirth, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生辰八字" align="center" prop="bazi" />
      <el-table-column label="亲人电话号码" align="center" prop="phoneNumber" />
      <el-table-column label="亲人电子邮件" align="center" prop="notificationMail" />
      <el-table-column label="亲人地址" align="center" prop="address" />
      <el-table-column label="亲人结婚纪念日" align="center" prop="anniversaryDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.anniversaryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="亲人特殊日期" align="center" prop="specialEventDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.specialEventDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="特殊日期描述" align="center" prop="specialEventDescription" />
      <el-table-column label="亲人照片链接" align="center" prop="photoUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.photoUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="爱好" align="center" prop="hobbies">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.qiu_user_info_hobbies" :value="scope.row.hobbies"/>
        </template>
      </el-table-column>
      <el-table-column label="亲人备注信息" align="center" prop="notes" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-info"
              @click="handleInfo(scope.row)"
              v-hasPermi="['base:info:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />



    <!-- 添加或修改存储情侣的亲人信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="68%" append-to-body>

      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="8">
        <el-form-item label="关联的情侣用户" prop="userId">
          <el-select v-model="form.userId"  placeholder="请输入关联情侣用户">
            <el-option
              v-for="item in userOptions"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            ></el-option>
          </el-select>
        </el-form-item>
        </el-col>
        <el-col :span="8">
        <el-form-item label="亲人姓名" prop="relativeName">
          <el-input v-model="form.relativeName" placeholder="请输入亲人姓名" />
        </el-form-item>
        </el-col>
          <el-col :span="8">
<!--        <el-form-item label="与情侣用户的关系" prop="relationToUser">-->
<!--          <el-input v-model="form.relationToUser" placeholder="请输入与情侣用户的关系" />-->
<!--        </el-form-item>-->
            <el-form-item label="与情侣用户的关系" prop="relationToUser">
              <el-select v-model="form.relationToUser" placeholder="请选择与情侣用户的关系">
                <el-option
                  v-for="dict in dict.type.qiu_user_info_relation"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
        <el-form-item label="亲人性别" prop="gender">
          <el-radio-group v-model="form.gender">
            <el-radio
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
          </el-col>
          <el-col :span="8">

        <el-form-item label="亲人出生日期" prop="dateOfBirth">
          <el-date-picker clearable
            v-model="form.dateOfBirth"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择亲人出生日期">
          </el-date-picker>
        </el-form-item>
          </el-col>
          <el-col :span="8">
        <el-form-item label="亲人电话号码" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入亲人电话号码" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
        <el-form-item label="亲人电子邮件" prop="email">
          <el-input v-model="form.email" placeholder="请输入亲人电子邮件" />
        </el-form-item>
          </el-col>
          <el-col :span="8">
        <el-form-item label="亲人地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入亲人地址" />
        </el-form-item>
          </el-col>
          <el-col :span="8">
        <el-form-item label="亲人结婚纪念日" prop="anniversaryDate">
          <el-date-picker clearable
            v-model="form.anniversaryDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择亲人结婚纪念日">
          </el-date-picker>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
        <el-form-item label="亲人特殊日期" prop="specialEventDate">
          <el-date-picker clearable
            v-model="form.specialEventDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择亲人特殊日期">
          </el-date-picker>
        </el-form-item>
          </el-col>
          <el-col :span="8">
        <el-form-item label="特殊日期描述" prop="specialEventDescription">
          <el-input v-model="form.specialEventDescription" placeholder="请输入特殊日期描述" />
        </el-form-item>
          </el-col>
          <el-col :span="8">
        <el-form-item label="亲人照片链接" prop="photoUrl">
          <image-upload v-model="form.photoUrl"/>
        </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="亲人备注信息" prop="notes">
          <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="爱好" prop="hobbies">
          <el-select v-model="form.hobbies" multiple placeholder="请选择爱好">
            <el-option
              v-for="dict in dict.type.qiu_user_info_hobbies"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生辰八字" prop="bazi">

          <el-date-picker @change="getBazi"
            v-model="form.baziTime"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
          <span>
            {{form.bazi}}
          </span>

        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

    </el-dialog>

    <!-- 存储情侣的亲人信息详情对话框 -->
    <el-dialog :title="title" :visible.sync="openInfo" width="68%" append-to-body>
      <el-descriptions border >

                        <el-descriptions-item label="关联情侣用户" >{{form.nickName}}</el-descriptions-item>
                        <el-descriptions-item label="亲人姓名" >{{form.relativeName}}</el-descriptions-item>
                   <el-descriptions-item label="与情侣用户的关系" >
                    <dict-tag :options="dict.type.qiu_user_info_relation" :value="form.relationToUser"/>
                        </el-descriptions-item>
                        <el-descriptions-item label="亲人性别" >
                          <dict-tag :options="dict.type.sys_user_sex" :value="form.gender"/>
                        </el-descriptions-item>
                        <el-descriptions-item label="亲人出生日期" >
                          {{ parseTime(form.dateOfBirth, '{y}-{m}-{d}')}}
                        </el-descriptions-item>
                      <el-descriptions-item label="生辰八字" >{{form.bazi}}</el-descriptions-item>
                        <el-descriptions-item label="亲人电话号码" >{{form.phoneNumber}}</el-descriptions-item>
                        <el-descriptions-item label="亲人电子邮件" >{{form.email}}</el-descriptions-item>
                        <el-descriptions-item label="亲人地址" >{{form.address}}</el-descriptions-item>
                        <el-descriptions-item label="亲人结婚纪念日" >
                          {{ parseTime(form.anniversaryDate, '{y}-{m}-{d}')}}
                        </el-descriptions-item>
                        <el-descriptions-item label="亲人特殊日期" >
                          {{ parseTime(form.specialEventDate, '{y}-{m}-{d}')}}
                        </el-descriptions-item>
                        <el-descriptions-item label="特殊日期描述" >{{form.specialEventDescription}}</el-descriptions-item>
                        <el-descriptions-item label="亲人照片链接">
                          <image-preview :src="form.photoUrl" :width="50" :height="50"/>
                        </el-descriptions-item>
                         <el-descriptions-item label="爱好" >
                      <dict-tag :options="dict.type.qiu_user_info_hobbies" :value="form.hobbies"/>
                      </el-descriptions-item>
                        <el-descriptions-item label="亲人备注信息" >{{form.notes}}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/base/info";
import {listUser} from "@/api/system/user";
import {getBirthGanzhi} from "@/utils/dateTool";
export default {
  name: "Info",
  dicts: ['sys_user_sex', 'qiu_user_info_hobbies', 'qiu_user_info_relation'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //
      userOptions:[],
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      openInfo: false,
      // 亲人备注信息时间范围
      daterangeSpecialEventDate: [],
      // 亲人备注信息时间范围
      daterangeCreateTime: [],
      // 亲人备注信息时间范围
      daterangeUpdateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        relativeName: null,
        relationToUser: null,
        gender: null,
        dateOfBirth: null,
        phoneNumber: null,
        email: null,
        address: null,
        anniversaryDate: null,
        specialEventDate: null,
        specialEventDescription: null,
        photoUrl: null,
        notes: null,
        bazi: null,
        hobbies: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "关联情侣用户ID不能为空", trigger: "blur" }
        ],
        relativeName: [
          { required: true, message: "亲人姓名不能为空", trigger: "blur" }
        ],
        relationToUser: [
          { required: true, message: "与情侣用户的关系不能为空", trigger: "blur" }
        ],
        gender: [
          { required: true, message: "亲人性别不能为空", trigger: "change" }
        ],
        dateOfBirth: [
          { required: true, message: "亲人出生日期不能为空", trigger: "blur" }
        ],
        email:[
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        phoneNumber: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getUserList();

  },

  methods: {
    /** 查询存储情侣的亲人信息列表 */
    getList() {
      console.log(getBirthGanzhi('2025-4-5 20:33'))
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeSpecialEventDate && '' != this.daterangeSpecialEventDate) {
        this.queryParams.params["beginSpecialEventDate"] = this.daterangeSpecialEventDate[0];
        this.queryParams.params["endSpecialEventDate"] = this.daterangeSpecialEventDate[1];
      }
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeUpdateTime && '' != this.daterangeUpdateTime) {
        this.queryParams.params["beginUpdateTime"] = this.daterangeUpdateTime[0];
        this.queryParams.params["endUpdateTime"] = this.daterangeUpdateTime[1];
      }
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },


    //关联情侣用户
    getUserList() {
      listUser({pageNum: 1, pageSize: 10000}).then(response => {
        this.userOptions = response.rows;
        console.log(this.userOptions)

      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openInfo = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        relativeName: null,
        relationToUser: null,
        gender: null,
        dateOfBirth: null,
        phoneNumber: null,
        email: null,
        address: null,
        anniversaryDate: null,
        specialEventDate: null,
        specialEventDescription: null,
        photoUrl: null,
        notes: null,
        createTime: null,
        updateTime: null,
        bazi: null,
        hobbies: null,
        baziTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeSpecialEventDate = [];
      this.daterangeCreateTime = [];
      this.daterangeUpdateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加存储情侣的亲人信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        // 将 hobbies 字符串转换为数组
        if (response.data.hobbies) {
          response.data.hobbies = response.data.hobbies.split(',');
        }
        this.form = response.data;
        this.open = true;
        this.title = "修改存储情侣的亲人信息";
      });
    },
    /** 详情按钮操作 */
    handleInfo(row) {
      this.reset();

      const id = row.id || this.ids
      getInfo(id).then(response => {

        // 将 hobbies 字符串转换为数组
        // if (response.data.hobbies) {
        //   response.data.hobbies = response.data.hobbies.split(',');
        // }

        this.form = response.data;
        this.openInfo = true;
        this.title = "存储情侣的亲人信息详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 将 hobbies 数组转换为逗号分隔的字符串
          if (Array.isArray(this.form.hobbies)) {
            this.form.hobbies = this.form.hobbies.join(',');
          }
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除存储情侣的亲人信息编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    },
    getBazi(value){
      console.log('bazi',getBirthGanzhi(value))
      this.form.bazi = getBirthGanzhi(value)
    }


  }
};
</script>
